﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DataAccessLayer.Models.POJO;
using DataAccessLayer.Mapper;
using DataAccessLayer.Service;
using Work04_01.Models.POJO;
using Work04_01.Models.ViewPOJO;
using System.Web.UI;

namespace Work04_01.Controllers
{
    public class userInfoController : Controller
    {

        userInfoService u = new userInfoService();
        departmentMapper dept = new departmentService();
        assetLevel1Mapper a1 = new assetLevel1Service();
        assetLevel2Mapper a2 = new assetLevel2Service();


        UserInfo now = null;
        Department department = null;
        userInfoPeta deptHost = null;
        bool LoginStatus = false;


        // GET: userInfo
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Filter()
        {
                return Content("<script>alert('非法操作！请登录！');window.location.href='Login';</script>");
            
        }

        public void getSessionInfo()
        {
            try
            {
                now = ((UserInfo)Session["now"]);
                department = ((Department)Session["department"]);
                deptHost = ((userInfoPeta)Session["deptHost"]);
                if(Session["LoginStatus"]!=null)
                LoginStatus = ((bool)Session["LoginStatus"]);
                
 
            }
            catch
            {
                return;
            }
            

        }

        public ActionResult ListAll()
        {
            try
            {
                List<userInfoPeta> list = u.SelectAllUserInfo();
                List<UserInfo> res = new List<UserInfo>();
                foreach(userInfoPeta user in list)
                {
                    res.Add(new UserInfo(user.userId,user.userName,user.password,user.deptId));
                }

                return View(res.ToArray());
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Login()
        { 
            return View();
        }

        [HttpPost]
        public ActionResult Login(Login login)
        {
           userInfoPeta user= u.SelectSingleUserInfoPetaById(login.userId);
            if (user == null)
            {
                return Content("未查询到该用户！");
            }
            else 
            {
                if (login.password.Equals(user.password))
                {
                    now= new UserInfo(user.userId,user.userName,user.password,user.deptId);
                    departmentPeta d = dept.SelectSingleInfoPetaById(user.deptId);
                    department = new Department(d.deptId,d.deptName,d.deptNumber,d.deptHostId);
                    deptHost = u.SelectSingleUserInfoPetaById(department.deptHostId);
                    deptHost.password = "***************";
                    Session["now"] = now;
                    Session["department"] = department;
                    Session["deptHost"] = deptHost;
                    Session["LoginStatus"] = true;
                    if(now.userId.Equals(department.deptHostId))
                    return RedirectToAction("UserIndex");
                    else return RedirectToAction("UserIndex02");
                }
                
                else return Content("<script>alert('密码错误！');window.location.href='Login';</script>");
            }
            
                
        }

        public ActionResult UserIndex()
        {
            if (Session["LoginStatus"] == null)
            {
                Filter();
            }
            return View();
        }

        public ActionResult MyUserInfo()
        {
            if (Session["LoginStatus"] == null)
            {
                Filter();
            }
            List<MyUserInfo> tar = new List<MyUserInfo>();
            tar.Add(new MyUserInfo((UserInfo)Session["now"], (Department)Session["department"] , (userInfoPeta)Session["deptHost"]));
            return View(tar.ToArray());
        }

        public ActionResult UpdatePassword(string userId)
        {
            if (Session["LoginStatus"] == null)
            {
                Filter();
            }
            return View();
        }

        [HttpPost]
        public ActionResult UpdatePassword(string userId, UserInfo userInfo)
        {
            try
            {
                // TODO: Add update logic here
                if (u.UpdatePassword(new userInfoPeta(((UserInfo)Session["now"]).userId, "", userInfo.password, "")))
                {
                    return Content("<script>alert('密码修改成功！请重新登录！');window.location.href='Login';</script>");
                   // return RedirectToAction("Login"); ;
                }

                else
                    return Content("<script>alert('密码修改失败！请联系管理员！');window.location.href='MyUserInfo';</script>");
            }
            catch(Exception ex)
            {
                return Content(ex.Message);
            }
        }
        public ActionResult MyDeparment()
        {
            if (Session["LoginStatus"] == null)
            {
                Filter();
            }
            getSessionInfo();
           
            if(now.userId.Equals(department.deptHostId))
            return RedirectToAction("MyDeparmentForHost");
            else return RedirectToAction("MyDeparmentForOrdinaryEmployees");
        }
        public ActionResult MyDeparmentForOrdinaryEmployees()
        {
            if (Session["LoginStatus"] == null)
            {
                Filter();
            }
            getSessionInfo();
            List<userInfoPeta> list = dept.SelectAllEmployee(now.deptId);
            List<MyUserInfo> res = new List<MyUserInfo>();

            foreach (userInfoPeta user in list)
            {
                res.Add(new MyUserInfo(new UserInfo(user.userId,user.userName,user.password,user.deptId),department,deptHost));
            }

            return View(res.ToArray());
        }
        public ActionResult MyDeparmentForHost()
        {
            if (Session["LoginStatus"] == null)
            {
                Filter();
            }
            getSessionInfo();
            List<userInfoPeta> list = dept.SelectAllEmployee(now.deptId);
            List<MyUserInfo> res = new List<MyUserInfo>();

            foreach (userInfoPeta user in list)
            {
                res.Add(new MyUserInfo(new UserInfo(user.userId, user.userName, user.password, user.deptId), department, deptHost));
            }

            return View(res.ToArray());
        }
        public ActionResult EditEmployeeInfo(String userId, String userName)
        {
            if (Session["LoginStatus"] == null)
            {
                Filter();
            }
            ViewBag.TGB = userId+'('+userName+')';
            return View();
        }
        [HttpPost]
        public ActionResult EditEmployeeInfo(String userId,String userName,UserInfo userinfo)
        {
            getSessionInfo();
            userinfo.userId = userId;
            userinfo.deptId = department.deptId;

            if (u.UpdateUserInfo(new userInfoPeta(userinfo.userId, userinfo.userName, userinfo.password, userinfo.deptId)))
                return Content("<script>alert('操作成功！');window.location.href='MyDeparmentForHost';</script>"); 
            else return Content("<script>alert('操作失败！请联系相关技术人员！');window.location.href='MyDeparmentForHost';</script>"); 
        }
        public ActionResult RemoveEmployee(String userId)
        {
            if (Session["LoginStatus"] == null)
            {
                Filter();
            }
            getSessionInfo();
            if (userId.Equals(department.deptHostId)) {
               return Content("<script>alert('您没有进行此操作的权限！');window.location.href='MyDeparmentForHost';</script>");
            }
            if (u.Delete(new userInfoPeta(userId, "", "", "")))
            {
                department.deptNumber--;
                if (dept.Update(new departmentPeta(department.deptId, department.deptName, department.deptNumber, department.deptHostId)))
                    return Content("<script>alert('操作成功！');window.location.href='MyDeparmentForHost';</script>");
                else
                {
                    department.deptNumber++;
                    return Content("<script>alert('操作失败！请联系相关技术人员！');window.location.href='MyDeparmentForHost';</script>");
                }

            }
            else return Content("<script>alert('操作失败！请联系相关技术人员！');window.location.href='MyDeparmentForHost';</script>");
        }
        public ActionResult AddEmployee()
        {
            if (Session["LoginStatus"] == null)
            {
                Filter();
            }
            return View();

        }
        [HttpPost]
        public ActionResult AddEmployee(UserInfo userInfo)
        {
            if (Session["LoginStatus"] == null)
            {
                Filter();
            }
            getSessionInfo();
            userInfoPeta user = new userInfoPeta(userInfo.userId,userInfo.userName,userInfo.password,department.deptId);
            if (u.Insert(user))
            {
                department.deptNumber++;
                if (dept.Update(new departmentPeta(department.deptId, department.deptName, department.deptNumber, department.deptHostId)))
                    return Content("<script>alert('添加成功！');window.location.href='MyDeparmentForHost';</script>");
                else
                {
                    department.deptNumber--;
                    return Content("<script>alert('操作失败！请联系相关技术人员！');window.location.href='MyDeparmentForHost';</script>");
                }
            }
            else
            {
                return Content("<script>alert('操作失败！该用户可能已经存在！');window.location.href='MyDeparmentForHost';</script>");
            }
            

        }
        public ActionResult DepartmentalAssets1()
        {
            if (Session["LoginStatus"] == null)
            {
                Filter();
            }
            getSessionInfo();
            try
            {
                List<assetLevel1Peta> list = a1.SelectAll(department.deptId);
                List<AssetLevel1> res = new List<AssetLevel1>();
                foreach (assetLevel1Peta a in list)
                {
                    res.Add(new AssetLevel1(a.assetLevel1Id,a.assetLevel1Name,a.ownerDeptId));
                }

                return View(res.ToArray());
            }
            catch
            {
                return View();
            }
        }
        public ActionResult CreateDepartmentalAssets1()
        {
            if (Session["LoginStatus"] == null)
            {
                Filter();
            }
            return View();
        }
        [HttpPost]
        public ActionResult CreateDepartmentalAssets1(AssetLevel1 a)
        {
            if (Session["LoginStatus"] == null)
            {
                Filter();
            }
            getSessionInfo();
            assetLevel1Peta ap = new assetLevel1Peta(a.assetLevel1Id,a.assetLevel1Name,department.deptId);
            if (a1.Insert(ap))
            {
                    return Content("<script>alert('添加成功！');window.location.href='DepartmentalAssets1';</script>");            }
            else
            {
                return Content("<script>alert('操作失败！该类别可能已经存在！');window.location.href='DepartmentalAssets1';</script>");
            }
        }
        public ActionResult DepartmentalAssets2(string assetLevel1Id,string ownerDeptId)
        {
            if (Session["LoginStatus"] == null)
            {
                Filter();
            }
            getSessionInfo();
            try
            {
                List<assetLevel2Peta> list = a2.SelectAll(assetLevel1Id , ownerDeptId);
                List<AssetLevel2> res = new List<AssetLevel2>();
                foreach (assetLevel2Peta a in list)
                {
                    res.Add(new AssetLevel2(a));
                }
                TempData["assetLevel1Id"] = assetLevel1Id;
                return View(res.ToArray());
            }
            catch
            {
                return View();
            }
        }
        public ActionResult AddDepartmentalAssets2(string assetLevel1Id)
        {
            if (Session["LoginStatus"] == null)
            {
                Filter();
            }
            TempData["assetLevel1Id"] = assetLevel1Id;
            return View();
        }
        [HttpPost]
        public ActionResult AddDepartmentalAssets2(string assetLevel1Id, AssetLevel2 a)
        {
            if (Session["LoginStatus"] == null)
            {
                Filter();
            }
            getSessionInfo();
            assetLevel2Peta ap = new assetLevel2Peta(assetLevel1Id, a.assetLevel2Id, a.assetName, department.deptId, a.ownerId, a.unit, a.number);
            if (a2.Insert(ap))
            {
                return Content("<script>alert('添加成功！');window.location.href='DepartmentalAssets1';</script>");
            }
            else
            {
                return Content("<script>alert('操作失败！该物品可能已经存在！');window.location.href='DepartmentalAssets1';</script>");
            }
        }

        public ActionResult UpdateDepartmentalAssets2(string assetLevel1Id, string assetLevel2Id)
        {
            if (Session["LoginStatus"] == null)
            {
                Filter();
            }
            return View();
        }
        [HttpPost]
        public ActionResult UpdateDepartmentalAssets2(string assetLevel1Id, string assetLevel2Id, AssetLevel2 a)
        {
            if (Session["LoginStatus"] == null)
            {
                Filter();
            }
            getSessionInfo();
            if(a.number<0) return Content("<script>alert('非法数据！');window.location.href='DepartmentalAssets1';</script>");
            try
            {
                if (a2.UpdateNumber(new assetLevel2Peta(assetLevel1Id,assetLevel2Id,"",department.deptId,"","",a.number)))
                {
                    return Content("<script>alert('物品数量修改成功！');window.location.href='DepartmentalAssets1';</script>");
                    
                }

                else
                    return Content("<script>alert('物品数量修改失败！请联系管理员！');window.location.href='DepartmentalAssets1';</script>");
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

        public ActionResult MyAssets2()
        {
            if (Session["LoginStatus"] == null)
            {
                Filter();
            }
            getSessionInfo();
            try
            {
                List<assetLevel2Peta> list = a2.SelectSomeSelf(now.userId);
                List<AssetLevel2> res = new List<AssetLevel2>();
                foreach (assetLevel2Peta a in list)
                {
                    res.Add(new AssetLevel2(a));
                }
                return View(res.ToArray());
            }
            catch
            {
                return View();
            }
        }

        public ActionResult UserIndex02()
        {
            if (Session["LoginStatus"] == null)
            {
                Filter();
            }
            return View();
        }
    }
}
/*

 
 */
